﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Kooboo.HtmlFunc.Providers;

namespace Kooboo.HtmlFunc.Analysis.Testers
{
    public class CommentTester : StringTester
    {
        static int PreCommentMarkerLength = "<!--".Length;
        static int SubCommentMarkerLength = "-->".Length;

        public CommentTester(string referenceText)
            : base(referenceText)
        {
        }

        public static string GetCommentText(HtmlCommentNode commentNode)
        {
            // eg: <!--position start-->

            //var comment = commentNode.Comment.Trim();
            //comment = comment.TrimStart("<!--".ToCharArray());
            //comment = comment.TrimEnd("-->".ToCharArray());
            //return comment;

            var comment = commentNode.Comment.Substring(PreCommentMarkerLength);
            return comment.Substring(0, comment.Length - SubCommentMarkerLength);
        }

        public override bool IsMatch(object real)
        {
            var commentNode = real as HtmlCommentNode;

            if (commentNode == null)
            {
                throw new ArgumentException();
            }

            var commentText = GetCommentText(commentNode);

            return base.IsMatch(commentText);
        }
    }
}
